package com.bbm.communitysystem.controller.post;


import cn.hutool.core.bean.BeanUtil;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.LikePost;
import com.bbm.communitysystem.domain.bo.LikePostBo;
import com.bbm.communitysystem.domain.dto.post.LikePostDto;
import com.bbm.communitysystem.service.impl.LikePostServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  点赞 控制器
 * </p>
 *
 * @author transitory
 * @since 2025-02-20
 */
@Tag(name = "点赞")
@RestController
@RequiredArgsConstructor
@RequestMapping("/like_post")
public class LikePostController {

    private final LikePostServiceImpl likePostService;

    /**
     * 增加点赞帖子 API
     * */
    @Operation(summary = "增加点赞帖子")
    @PostMapping
    public R<String> addLikePost(@RequestBody LikePostDto likePostDto) {

        LikePost likePost = BeanUtil.copyProperties(likePostDto, LikePost.class);
        likePost.setCreateTime(System.currentTimeMillis() + "");
        likePost.setType(0);

        return likePostService.likePost(likePost) == 0 ? R.ok("success") : R.error("fail");
    }

    @Operation(summary = "增加点赞评论")
    @PostMapping("/comment")
    public R<String> addLikeComment(@RequestBody LikePostDto likePostDto) {

        LikePostBo l = new LikePostBo();

        l.setType(1);
        l.setSourceId(likePostDto.getSourceId());
        l.setFounder(likePostDto.getFounder());
        l.setStatus(1);
        l.setPostId(likePostDto.getSourceId());
        l.setCommentId(likePostDto.getCommentId());

        return likePostService.likeComment(l) == 0 ? R.ok("success") : R.error("fail");
    }

    /**
     * 删除点赞 API
     * */

    @Operation(summary = "删除点赞帖子")
    @DeleteMapping("/{id}")
    @Parameter(name = "id", description = "点赞id")
    public R<String> deleteLikePost(@PathVariable("id") Integer id) {
        return likePostService.delLikePost(id) == 0 ? R.ok("success") : R.error("fail");
    }


    @Operation(summary = "删除点赞2帖子")
    @DeleteMapping("/")
    @Parameters({
            @Parameter(name = "postId", description = "帖子id"),
            @Parameter(name = "userId", description = "用户id")
    })
    public R<String> deleteLikePost(@RequestParam("postId") Integer id, @RequestParam("userId") Integer userId) {
        LikePostBo likePostBo = new LikePostBo();
        likePostBo.setSourceId(id);
        likePostBo.setFounder(userId);
        likePostBo.setType(0);
        return likePostService.delLikePost(likePostBo) == 0? R.ok("success") : R.error("fail");
    }

    @Operation(summary = "删除评论点赞")
    @DeleteMapping("/comment/{id}")
    @Parameters({
            @Parameter(name = "id", description = "点赞id")
    })
    public R<String> deleteLikeComment(@PathVariable("id") Integer id) {

        boolean update = likePostService.lambdaUpdate()
                .eq(LikePost::getId, id)
                .set(LikePost::getStatus, 0)
                .update();

        return update ? R.ok() : R.error();
    }

    @Operation(summary = "删除评论点赞1")
    @DeleteMapping("/comment")
    public R<String> deleteLikeComment(@RequestParam("commentId") Integer id,
                                       @RequestParam("userId") Integer userId) {

        LikePostBo likePostBo = new LikePostBo();
        likePostBo.setCommentId(id);
        likePostBo.setFounder(userId);
        likePostBo.setType(1);

        int i = likePostService.delLikeComment(likePostBo);

        return i == 0 ? R.ok() : R.error();
    }

}
